﻿using Microsoft.EntityFrameworkCore;
using Rbac.Domain;
using Rbac.Project.Domain;
using System;

namespace Rabc.Project.Repositories
{
    public class RbacDbContext : DbContext
    {
        public RbacDbContext(DbContextOptions options) : base(options)
        {

        }

        public DbSet<Admin> Admins { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Menu> Menus { get; set; }
        public DbSet<AdminRole> AdminRoles { get; set; }
        public DbSet<RoleMenu> RoleMenus { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Admin>(build => {
                build.Property(m => m.UserName).HasMaxLength(50).IsRequired();
                build.Property(m => m.Password).HasMaxLength(50).IsRequired();
                build.Property(m => m.FullName).HasMaxLength(50);
                build.Property(m => m.Email).HasMaxLength(50);
                build.Property(m => m.LastLoginIP).HasMaxLength(50);
                build.Property(m => m.Avatar).HasMaxLength(100);
            });

            modelBuilder.Entity<Role>(build => {
                build.Property(m => m.RoleName).HasMaxLength(50).IsRequired();
                build.Property(m => m.Remark).HasMaxLength(500);
            });

            modelBuilder.Entity<Menu>(build => {
                build.Property(m => m.MenuName).HasMaxLength(50).IsRequired();
                build.Property(m => m.MenuRoute).HasMaxLength(200);
                build.Property(m => m.MenuAPI).HasMaxLength(200);
                build.Property(m => m.Icon).HasMaxLength(50);
                build.Property(m => m.Component).HasMaxLength(50);
                build.Property(m => m.RouteName).HasMaxLength(50);
                build.Property(m => m.Redirect).HasMaxLength(500);
            });

            modelBuilder.Entity<AdminRole>(build => {
                build.HasOne<Admin>().WithMany().HasForeignKey(m => m.AdminId);
                build.HasOne<Role>().WithMany().HasForeignKey(m => m.RoleID);
            });

            modelBuilder.Entity<RoleMenu>(build => {
                build.HasOne<Role>().WithMany().HasForeignKey(m => m.RoleID);
                build.HasOne<Menu>().WithMany().HasForeignKey(m => m.MenuID);
            });


            base.OnModelCreating(modelBuilder);
        }
    }
}
